package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Business;
import com.example.service.BusinessService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/business")
public class BusinessController {
    @Resource
    BusinessService businessService;
    @PostMapping("/add")
    public Result add(@RequestBody Business business){
        if(ObjectUtil.isEmpty(business.getUsername())||ObjectUtil.isEmpty(business.getPassword())){
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        businessService.add(business);
        return Result.success();
    }
    @PutMapping("/update")
    public Result update(@RequestBody Business business){
        businessService.updateById(business);
        return Result.success();
    }
    @GetMapping("/selectAll")
    public Result selectAll(Business business){
        List<Business> list = businessService.selectAll(business);
        return Result.success(list);
    }
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Business dbBusiness = businessService.selectById(id);
        return Result.success(dbBusiness);
    }
    @GetMapping("/selectPage")
    public Result selectById(Business business,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Business> pageInfo = businessService.selectPage(business, pageNum, pageSize);
        return Result.success(pageInfo);
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        businessService.deleteById(id);
        return Result.success();
    }
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer>ids){
        businessService.deleteBatch(ids);
        return Result.success();
    }
}
